class Pipe {
    constructor(type = 0) {
        // this.type = ~~(Math.random() + 0.5); //0为朝上，1为朝下
        this.type = type;
        this.width = 72;
        this.height = Math.random() * ((game.canvas.height >> 1) - 100) + 100;
        //管子图片起始切片宽度
        this.startWidth = 175;
        //地板以上画布高度
        // this.overFloorHeight = 421;
        this.overFloorHeight = game.canvas.height - game.floor.height
        this.x = game.canvas.width;
        this.y = this.type === 0 ? this.overFloorHeight - this.height : 0;
        this.speed = 3;
    }
    update() {
        this.x -= this.speed;
        //碰撞检测
        //进入当前管子
        if (game.bird.x > this.x - game.bird.width && game.bird.x < this.x + this.width) {
            if (this.type === 0) {
                if (game.bird.y + game.bird.height >= this.y) {
                    game.audio.setAttribute('src', 'music/No.mp3');
                    game.audio.load();
                    game.audio.play();
                    game.suspend();
                    game.restart();
                }
            } else if (this.type === 1) {
                if (game.bird.y <= this.height) {
                    game.audio.setAttribute('src', 'music/No.mp3');
                    game.audio.load();
                    game.audio.play();
                    game.suspend();
                    game.restart();
                }
            }
        }

        //加分
        // if(this.x < canvas.width / 2 - this.width && !this.done){
        //     game.scoreManager.score++;
        //     this.done = true;
        //     const audio = document.getElementsByTagName("audio")[0];
        //     audio.load();
        //     audio.play();
        // }



        if (this.x < game.bird.x - this.width && !this.done) {
            game.scoreManager.score++;
            this.done = true;
            game.audio.load();
            game.audio.play();
        }





    }
    render() {
        if (this.type === 0) {
            //向上的管子
            // console.log(this.height);
            game.pen.drawImage(game.images.pipe1, this.startWidth, 0, this.width, this.height, this.x, this.overFloorHeight - this.height, this.width, this.height);


        } else if (this.type === 1) {
            //向下的管子
            game.pen.drawImage(game.images.pipe2, this.startWidth, 420 - this.height, this.width, this.height, this.x, 0, this.width, this.height);

        }

    }
    pause() {
        this.speed = 0;
    }






}